<!DOCTYPE html>
<html>

<head lang="en">
    <title>找攻略</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategy.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css">
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>
    <script>
        $(function () {
            //获取攻略推荐游记
            $.get("/travelCommends", {type: 3, pageSize: 1}, function (data) {
                $(".commend").renderValues(data.list[0], {
                    getHref: function (item, value) {
                        var url = $(item).data("href");
                        $(item).attr("href", url + value)
                    }
                });
            });
            //获取推荐大标题
            $.get("/strategies",{pageSize:0,state:1},function (data) {
                $(".strategyCommend").renderValues(data,{
                    getHref: function (item, value) {
                        var url = $(item).data("href");
                        $(item).attr("href", url + value)
                    }
                });
            });


            var regionId;//当前浏览的id
            //获取推荐地区
            $.get("/regions",{state:1},function (data) {
                var temp = "";
                var content= "";
                $.each(data,function (index,ele) {
                    temp += '<li class="nav-item"><a data-rid="'+ele.id+'" class="nav-link" id="pills-1-tab" ' +
                        'data-toggle="pill" href="#pills-'+ele.id+'">'+ele.name+'</a></li>';
                    content +='<div class="tab-pane fade" id="pills-'+ele.id+'"></div>'
                });
                //添加到标题
                $("#pills-tab").append(temp);
                //添加到内容
                $("#pills-tabContent").append(content);
                $(".nav-link").click(function () {
                   var rid =  $(this).data("rid");
                   //赋值给成员变量
                   regionId=rid;
                   //点击标题时,重置当前页和总页数
                   currentPage = 2;//当前页
                   pages=2;//总页数
                   // 如果地区有id,才获取数据
                   if(regionId) {
                       $.get('/regions/' + rid + '/strategies', function (data) {
                           //console.log(data);
                           //这里不能使用append,如果使用,会一直因为值改变,而导致数据不断往后面增加
                           $('#pills-' + rid).html(data);
                       }, "html")
                   }
                })


            });

            var currentPage = 2;//当前页
            var pages=2;//总页数
            function query(){
                //如果地区有id,才获取数据
                if(regionId){
                    $.get('/regions/' + regionId + '/strategies', {currentPage: currentPage}, function (data) {
                        var temp = "";
                        $.each(data.list, function (index, ele) {
                            temp += '<div class="col-6 mb">\n' +
                                '                    <a href="../strategyCatalogs.html?id=' + ele.id + '">\n' +
                                '                        <img class="float-left " src="' + ele.coverUrl + '">\n' +
                                '                        <div class="classify-text">\n' +
                                '                            <span>' + ele.title + '</span>\n' +
                                '                            <p>0人收藏</p>\n' +
                                '                        </div>\n' +
                                '                    </a>\n' +
                                '                </div>'
                        });
                        $(".classify").append(temp);
                        pages = data.pages;
                    }, "json");
                    currentPage++;
                }
            }
            //默认不需要查第一页,因为在第一页的内容是通过模板渲染出来的,我们要从第二页开始查
            //监听滚动事件
            $(window).scroll(function () {
                //当滚上去的高度与手机窗口的高度大于等于html文档高度的时候,触发分页的操作
                if ($(document).scrollTop() + $(window).height() >= $(document).height()-1) {//有时候会有像素偏差,需要根据需求适当的减少文档高度
                    //判断当前页是否小于或等于总页数,如果是,就执行查询
                    if (pages >= currentPage) {
                        //执行分页查询
                        query();
                    }else {
                        $(document).dialog({
                            type: 'notice',
                            infoText: '已经到底了',
                            autoClose: 1500,
                            position: 'bottom'  // center: 居中; bottom: 底部
                        });
                    }
                }
            });
        })
    </script>

</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="index.html">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="找攻略">
            </div>
        </div>
    </div>
</div>

<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab" role="tablist">
    <li class="nav-item">
        <a class="nav-link active show" id="pills-top-tab" data-toggle="pill" href="#pills-top">
            <span style="font-weight: bold;">推荐</span>
        </a>
    </li>

</ul>

<div class="tab-content" id="pills-tabContent">
    <div class="tab-pane fade active show" id="pills-top">
        <div class="container commend">
            <a data-href="travelContent.html?id=" render-key="travel_id" render-fun="getHref">
                <img render-src="coverUrl">
                <p render-html="title"></p>
            </a>
        </div>
        <hr>
        <div class="container strategyCommend">
            <h6>当季推荐</h6>
            <div class="row hot" render-loop="list">
                <div class="col-4">
                    <a data-href="strategyCatalogs.html?id=" render-key="list.id" render-fun="getHref">
                        <img render-src="list.coverUrl">
                        <p render-html="list.title"></p>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>

</script>
</html>